package com.qqtech.qquan.circle.service;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.base.Strings;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Lists;
import com.qqtech.core.common.cache.Cache;
import com.qqtech.core.common.util.SettingCfgUtil;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.dao.CircleSysDictDao;
import com.qqtech.qquan.circle.model.CircleSysDict;

@Service
public class CircleSysDictServiceImpl extends BaseServiceImpl<CircleSysDict>
		implements CircleSysDictService, Cache<String, List<CircleSysDict>> {
	public static final Logger log = LoggerFactory.getLogger(CircleSysDictServiceImpl.class);

	@Resource
	private CircleSysDictDao circleSysDictDao;

	@Override
	protected BaseDao<CircleSysDict> getBaseDao() {
		return circleSysDictDao;
	}

	private LoadingCache<String, List<CircleSysDict>> caches = CacheBuilder.newBuilder().recordStats()
			.expireAfterWrite(
					SettingCfgUtil.getConfigHelper("qquan-cfg.properties").getIntValue("dictcache.expire.mins"),
					TimeUnit.MINUTES)
			.build(new CacheLoader<String, List<CircleSysDict>>() {
				public List<CircleSysDict> load(String dictType) {
					CircleSysDict circleSysDict = new CircleSysDict();
					circleSysDict.setType(dictType);
					List<CircleSysDict> circleSysDictList = circleSysDictDao.queryList(circleSysDict);
					if (circleSysDictList != null) {
						return circleSysDictList;
					}
					return Lists.newArrayList();
				}
			});
	
	@Override
	public String dictValue(String dictType, String dictKey) {
		if (Strings.isNullOrEmpty(dictType) || Strings.isNullOrEmpty(dictKey)) {
			return "";
		}
		List<CircleSysDict> dicts = caches.getUnchecked(dictType);
		for (CircleSysDict dict : dicts) {
			if (dictKey.equals(dict.getCode())) {
				return dict.getName();
			}
		}
		return "";
	}

	@Override
	public List<CircleSysDict> get(String key) {
		return caches.getUnchecked(key);
	}

	@Override
	public void clear(String dictType) {
		caches.invalidate(dictType);
	}

	@Override
	public void clearAll() {
		caches.invalidateAll();
	}

	@Override
	public int delete(CircleSysDict query) {
		if (query != null && StringUtil.isNotBlank(query.getType())) {
			this.clear(query.getType());
		}
		return super.delete(query);
	}

	@Override
	public int deleteAll() {
		clearAll();
		return super.deleteAll();
	}

	@Override
	public int deleteById(Integer id) {
		CircleSysDict circleSysDict = getById(id);
		if (circleSysDict != null && StringUtil.isNotBlank(circleSysDict.getType())) {
			this.clear(circleSysDict.getType());
		}
		return super.deleteById(id);
	}

	@Override
	public int insert(CircleSysDict entity) {
		if (entity != null && StringUtil.isNotBlank(entity.getType())) {
			this.clear(entity.getType());
		}
		return super.insert(entity);
	}

	@Override
	public int updateById(CircleSysDict entity) {
		if (entity != null && StringUtil.isNotBlank(entity.getType())) {
			this.clear(entity.getType());
		}
		return super.updateById(entity);
	}
}